/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package idp.utils;

import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.text.NumberFormat;
import java.util.Calendar;
import java.util.Vector;
import java.util.logging.ConsoleHandler;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.WindowConstants;
import javax.swing.table.TableColumnModel;

/**
 *
 * @author admin
 */
public class LogController extends ConsoleHandler
{
    private LogView _view;

    public LogController(String LogClass)
    {
        super();
        _view = new LogView();
        Logger.getLogger(LogClass).addHandler(this);
    }

    public LogView getView()
    {
        return _view;
    }

    /**
     * pushes log messages to the screen
     * @param r
     */
    @Override
    public void publish(LogRecord r)
    {
        flush();
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(r.getMillis());
        NumberFormat nf = NumberFormat.getInstance(); // Get Instance of NumberFormat
        nf.setMinimumIntegerDigits(2);  // The minimum Digits required is 5
        nf.setMaximumIntegerDigits(2); // The maximum Digits required is 5


        Vector v = new Vector();
        v.add(r.getLevel().toString());
        v.add(c.get(12) + ":" + nf.format(c.get(13)));
        v.add(r.getSourceClassName());
        v.add(r.getSourceMethodName());
        v.add(r.getMessage());

        _view.add(v);
    }

    private class LogView extends JFrame
    {

        private Vector<Vector> logData;
        private JScrollPane pane;
        private JTable table;
        private JScrollBar vBar;

        /**
         * Constructor
         */
        public LogView()
        {
            super("Logbook");

            initComponents();

            setSize(600, 400);
            setVisible(true);
        }

        /**
         * Initialises the main window
         */
        private void initComponents()
        {
            Vector<String> logInfo = new Vector();
            logInfo.add("");
            logInfo.add("Time");
            logInfo.add("Class");
            logInfo.add("Method");
            logInfo.add("Message");

            logData = new Vector();
            table = new JTable(logData, logInfo);
            table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);

            TableColumnModel c = table.getColumnModel();
            c.getColumn(0).setMaxWidth(20);
            c.getColumn(0).setResizable(false);
            c.getColumn(1).setMaxWidth(50);
            c.getColumn(1).setResizable(false);
            c.getColumn(2).setMaxWidth(120);
            c.getColumn(2).setResizable(false);
            c.getColumn(3).setMaxWidth(160);
            c.getColumn(3).setResizable(true);

            pane = new JScrollPane(table);
            setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

            vBar = pane.getVerticalScrollBar();
            vBar.addAdjustmentListener(new AdjustmentListener()
            {

                public void adjustmentValueChanged(AdjustmentEvent e)
                {

                    //System.out.println(vBar.getValue() + "/" + vBar.getMaximum() + " " + );


                    if (!e.getValueIsAdjusting())
                    {
                        if (vBar.getValue() >= (vBar.getMaximum() - vBar.getVisibleAmount() - 50))
                        {

                            vBar.setValue(vBar.getMaximum());

                        }
                    }
                }
            });

            add(pane);

            //Set window characteristics
            this.pack();                      // Layout components.
            this.setLocationRelativeTo(null); // Center window.
        }

        /**
         * Adds a array of logdata to the JTable
         * @param a Logdata
         */
        public void add(Vector a)
        {
            if (logData.size() > 200)
            {
                logData.removeElementAt(0);
            }
            logData.add(a);

            table.updateUI();
        }

    }
}
